﻿function AdviserByCurrentSession() {

    this.netScoreToAlertTimeMap = {
        0: 1000,// "expected utility = -142.290128
        1: 3000,// "expected utility = -97.175557
        2: 4000,// "expected utility = -25.542989
        3: 5000,// "expected utility = 62.518141
        4: 6000,// "expected utility = 163.489162
        5: 7000,// "expected utility = 274.775415
        6: 8000,// "expected utility = 394.415841
        7: 9000,// "expected utility = 520.895464
        8: 10000,// "expected utility = 653.020569
        9: 11000,// "expected utility = 789.833369
        10: 11000,// "expected utility = 931.918699
        11: 12000,// "expected utility = 1077.956977
        12: 13000,// "expected utility = 1226.775323
        13: 13000,// "expected utility = 1379.021381
        14: 14000,// "expected utility = 1534.569452
        15: 15000,// "expected utility = 1691.648654
        16: 15000,// "expected utility = 1852.395855
        17: 16000,// "expected utility = 2014.452630
        18: 16000,// "expected utility = 2178.959601
        19: 17000,// "expected utility = 2345.167272
        20: 17000,// "expected utility = 2513.160220
        21: 18000,// "expected utility = 2682.816075
        22: 18000,// "expected utility = 2854.053632
        23: 19000,// "expected utility = 3026.553978
        24: 19000,// "expected utility = 3200.822461
        25: 20000,// "expected utility = 3375.643109
        26: 20000,// "expected utility = 3552.752638
        27: 20000,// "expected utility = 3729.862166
        28: 21000,// "expected utility = 3909.214743
        29: 21000,// "expected utility = 4088.996026
        30: 22000,// "expected utility = 4269.648989
        31: 22000,// "expected utility = 4451.950667
        32: 22000,// "expected utility = 4634.252345
        33: 23000,// "expected utility = 4818.239395
        34: 23000,// "expected utility = 5002.925808
        35: 23000,// "expected utility = 5187.612221
        36: 24000,// "expected utility = 5374.370299
        37: 24000,// "expected utility = 5561.319621
        38: 24000,// "expected utility = 5748.268943
        39: 25000,// "expected utility = 5937.296868
        40: 25000,// "expected utility = 6126.399525
        41: 25000,// "expected utility = 6315.502182
        42: 26000,// "expected utility = 6506.349836
        43: 26000,// "expected utility = 6697.507174
        44: 26000,// "expected utility = 6888.664512
        45: 27000,// "expected utility = 7080.923810
        46: 27000,// "expected utility = 7274.046958
        47: 27000,// "expected utility = 7467.170106
        48: 28000,// "expected utility = 7660.467400
        49: 28000,// "expected utility = 7855.476307
        50: 28000,// "expected utility = 8050.485213
        51: 28000,// "expected utility = 8245.494119
        52: 29000,// "expected utility = 8441.297421
        53: 29000,// "expected utility = 8638.120032
        54: 29000,// "expected utility = 8834.942642
        55: 29000,// "expected utility = 9031.765252
        56: 30000,// "expected utility = 9229.621713
        57: 30000,// "expected utility = 9428.193270
        58: 30000,// "expected utility = 9626.764827
        59: 30000,// "expected utility = 9825.336385
        60: 31000,// "expected utility = 10024.830715
        61: 31000,// "expected utility = 10225.093165
        62: 31000,// "expected utility = 10425.355615
        63: 31000,// "expected utility = 10625.618065
        64: 32000,// "expected utility = 10826.366152
        65: 32000,// "expected utility = 11028.267641
        66: 32000,// "expected utility = 11230.169129
        67: 32000,// "expected utility = 11432.070618
        68: 32000,// "expected utility = 11633.972106
        69: 33000,// "expected utility = 11837.208395
        70: 33000,// "expected utility = 12040.702845
        71: 33000,// "expected utility = 12244.197295
        72: 33000,// "expected utility = 12447.691745
        73: 34000,// "expected utility = 12651.444760
        74: 34000,// "expected utility = 12856.491520
        75: 34000,// "expected utility = 13061.538281
        76: 34000,// "expected utility = 13266.585041
        77: 34000,// "expected utility = 13471.631802
        78: 35000,// "expected utility = 13677.101828
        79: 35000,// "expected utility = 13883.665389
        80: 35000,// "expected utility = 14090.228950
        81: 35000,// "expected utility = 14296.792511
        82: 35000,// "expected utility = 14503.356072
        83: 36000,// "expected utility = 14710.177322
        84: 36000,// "expected utility = 14918.227088
        85: 36000,// "expected utility = 15126.276855
        86: 36000,// "expected utility = 15334.326621
        87: 36000,// "expected utility = 15542.376387
        88: 36000,// "expected utility = 15750.426153
        89: 37000,// "expected utility = 15959.715607
        90: 37000,// "expected utility = 16169.225727
        91: 37000,// "expected utility = 16378.735847
        92: 37000,// "expected utility = 16588.245966
        93: 37000,// "expected utility = 16797.756086
        94: 38000,// "expected utility = 17007.707187
        95: 38000,// "expected utility = 17218.656436
        96: 38000,// "expected utility = 17429.605686
        97: 38000,// "expected utility = 17640.554935
        98: 38000,// "expected utility = 17851.504184
        99: 38000,// "expected utility = 18062.453434
        100: 39000,// "expected utility = 18274.182952
    };
    
    this.freezeTime = 0;
    this.alertDisplayed = false;
    this.responseDelayContainer = [];
    this.lastAlertTime = 0;

    this.avgResponseDelay = 2000;

    this.onAction = function (time, action, mc) {
        if (action == enumAction.MAINTENANCE || action == enumAction.MALFUNCTION_OCCUR) {
            if (this.alertDisplayed == true) {
                responseDelay = time - this.lastAlertTime - this.freezeTime;
                this.addResponseDelay(responseDelay);
            }
            this.alertDisplayed = false;
        }
    }

    this.onSpaceshipFreeze = function (time, freezeDuration) {
        this.freezeTime += freezeDuration;
    }

    this.isTimeToDisplayAlert = function (netScore, netTimeFromLastEvent, time) {
        if (this.alertDisplayed == true) {
            return false;
        }
        recomendedTime = this.getRecomendedAlertTime(netScore);
        if (recomendedTime <= netTimeFromLastEvent) {
            this.alertDisplayed = true;

            this.lastAlertTime = time;
            this.freezeTime = 0;

            return true;
        }
        return false;
    }

    this.addResponseDelay = function (responseDelay) {
        if (responseDelay > 8000) {
            return;
        }
        this.responseDelayContainer.push(responseDelay);

        // calculate average
        sumOfDelays = 0;
        for (i = 0; i < this.responseDelayContainer.length; i++) {
            sumOfDelays += this.responseDelayContainer[i];
        }

        this.avgResponseDelay = sumOfDelays / this.responseDelayContainer.length;
    }

    this.getResponseDelay = function () {
        //return this.avgResponseDelay;
        return 1000; // TODO: remove after halfPesonalize experiment
    }

    this.getFixRecommendedTime = function (netScore) {
        key = Math.round(netScore);
        key = Math.max(key, 5);
        key = Math.min(key, 100);
        fixRecomnededTime = this.netScoreToAlertTimeMap[key];
        return fixRecomnededTime;
    }

    this.getRecomendedAlertTime = function (netScore) {
        return this.getFixRecommendedTime(netScore) - this.getResponseDelay();
    }

}
AdviserByCurrentSession.prototype = new GameEventListener();


function AdviserByFixTime(fixTime) {

    this.fixTime = fixTime;

    this.getFixRecommendedTime = function (netScore) {
        return this.fixTime;
    }

    this.getRecomendedAlertTime = function (netScore) {
        return this.fixTime - 3000;
    }
}
AdviserByFixTime.prototype = new AdviserByCurrentSession();

function NonAdviser() {
    this.isTimeToDisplayAlert = function (netScore, netTimeFromLastEvent, time) {
        return false;
    }

    this.getRecomendedAlertTime = function (netScore) {
        return undefined;
    };

    this.getFixRecommendedTime = function (netScore) {
        return undefined;
    }
}

NonAdviser.prototype = new AdviserByCurrentSession();




